package pl.edu.mimuw.mas.agent.behaviour.hunting.hunter;

import jade.core.behaviours.SequentialBehaviour;
import jade.core.behaviours.SimpleBehaviour;
import pl.edu.mimuw.mas.agent.HunterAgent;
import pl.edu.mimuw.mas.agent.behaviour.hunting.grouping.GroupingBehaviour;

public class ScaringBehaviour extends SequentialBehaviour {

	private static final long serialVersionUID = -8886975632575870584L;

	public ScaringBehaviour(HunterAgent a) {
		super(a);
		// grupowanie
		addSubBehaviour(new GroupingBehaviour(a));

		// parowanie (tylko jeśli liderem)
		addSubBehaviour(new SimpleBehaviour(a) {

			private static final long serialVersionUID = -8776338699053336260L;

			private SequentialBehaviour b = null;
			private boolean finished = false;

			@Override
			public void action() {
				HunterAgent a = (HunterAgent) myAgent;

				// to zachowanie jest tylko dla liderow
				if (!a.isLeader()) {
					finished = true;
					return;
				} else if (b == null){
					b = new GroupPairingBehaviour(a);
					a.addBehaviour(b);
				}

			}

			@Override
			public boolean done() {
				if (b == null)
					return finished;
				else
					return b.done();
			}

		});

		// wybranie najlepszej pary
		addSubBehaviour(new SimpleBehaviour(a) {

			private static final long serialVersionUID = -8776338699053336260L;

			private SequentialBehaviour b = null;
			private boolean finished = false;

			@Override
			public void action() {
				HunterAgent a = (HunterAgent) myAgent;

				// to zachowanie jest tylko dla liderow
				if (!a.isLeader()) {
					finished = true;
					return;
				} else if (b == null) {
					b = new BestPairChoosingBehaviour(a);
					a.addBehaviour(b);
				}
				;

			}

			@Override
			public boolean done() {
				if (b == null)
					return finished;
				else
					return b.done();
			}

		});

	}
}
